package com.biao.synchronized_test.test_lock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Test02_condition {
    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();

    private void await() {
        new Thread(() -> {
            try {
                lock.lock();
                System.out.println("线程休眠阻塞");
                condition.await();      // 让线程阻塞等待，等于wait意思
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } finally {
                lock.unlock();
            }
        }).start();
    }

    private void signal() {
        try {
            lock.lock();
            condition.signal();
            System.out.println("线程唤醒");
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            lock.unlock();
        }

    }

    public static void main(String[] args) {
        Test02_condition test02Condition = new Test02_condition();
        test02Condition.await();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        test02Condition.signal();
    }
}
